from typing import Optional
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession

from app.crud.base import CRUDBase
from app.models.report_classification import ReportClassification
from app.schemas.report_classification import ReportClassificationCreate, ReportClassificationUpdate


class CRUDReportClassification(CRUDBase[ReportClassification, ReportClassificationCreate, ReportClassificationUpdate]):
    async def get_by_name(self, db: AsyncSession, *, name: str) -> Optional[ReportClassification]:
        """Get report classification by name"""
        result = await db.execute(select(ReportClassification).filter(ReportClassification.name == name))
        return result.scalars().first()


report_classification = CRUDReportClassification(ReportClassification)